package com.cloud.wms.ui.group.collect;

import android.os.Message;
import android.view.View;
import android.widget.ImageButton;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cloud.core.eventBus.Event;
import com.cloud.widget.ScanText;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarListActivity;
import com.cloud.wms.config.EventConfig;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.ui.group.contract.GroupCollectContract;
import com.cloud.wms.ui.group.entity.GroupBatchInfoEntity;
import com.cloud.wms.ui.group.presenter.GroupCollectPresenter;
import com.cloud.wms.ui.main.entity.TaskEntity;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @Author:Chen
 * @Date: 2021-01-20 15:22
 * @Description: 波次单列表
 */
public class GroupBatchActivity extends BaseBarListActivity<GroupBatchInfoEntity, GroupCollectPresenter> implements GroupCollectContract.View {
    @BindView(R.id.iv_search)
    ImageButton ivSearch;
    @BindView(R.id.scan_text)
    ScanText scanText;
    private String taskCode;

    @Override
    protected String barTitle() {
        return "集货波次";
    }

    @Override
    protected int getResId() {
        return R.layout.activity_sort_batch;
    }

    @Override
    protected int getItemResId() {
        return R.layout.item_sort_batch;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        initScanText(scanText);
        taskCode = getIntent().getStringExtra(IntentKey.INFO_KEY);
        mPresenter.loadBatchList("", page = 0);
    }

    @Override
    public void onReload() {
        super.onReload();
        mPresenter.loadBatchList("", page = 0);
    }

    @Override
    protected boolean isRefresh() {
        return true;
    }

    @Override
    protected boolean isLoadMore() {
        return true;
    }

    @Override
    protected void loadListData(int page) {
        super.loadListData(page);
        mPresenter.loadBatchList(scanText.getText(), page);
    }

    @Override
    protected void onScanHandle(String result, int resultCode) {
        super.onScanHandle(result, resultCode);
        scanText.addHistory(result);
        mPresenter.loadBatchList(result, page = 0);
    }

    @Override
    protected void bindItemData(BaseViewHolder helper, GroupBatchInfoEntity item) {
        helper.setText(R.id.tv_batch_code, item.getBatchCode())
                .setText(R.id.tv_batch_type, item.getBatchTypeName())
                .setText(R.id.tv_batch_sku, item.getSkuAmount())
                .setText(R.id.tv_batch_amount, item.getTotalAmount())
                .setText(R.id.tv_batch_create, item.getCreateDate());
    }

    @Override
    protected void itemClickListener(BaseQuickAdapter<GroupBatchInfoEntity, BaseViewHolder> mAdapter, View view, int position) {
        super.itemClickListener(mAdapter, view, position);
        mPresenter.receiveTask(mAdapter.getData().get(position).getBatchID(), taskCode);
    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }

    @Override
    public void onSuccess(Message message) {
        super.onSuccess(message);
        switch (message.what) {
            case GroupCollectPresenter.TASK:
                TaskEntity taskEntity = (TaskEntity) message.obj;
                GroupCollectActivity.start(this, taskEntity.getBillID() + "", taskEntity);
                break;
        }
    }

    @OnClick({R.id.iv_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_search:
                String text = scanText.getText();
                mPresenter.loadBatchList(text, page = 0);
                break;
        }
    }

    @Override
    protected boolean isUseEventBus() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    public void onEventCome(Event event) {
        if (event == null) {
            return;
        }
        switch (event.getCode()) {
            case EventConfig.CLOSE_REFRESH:
                mPresenter.loadBatchList("", page = 0);
                break;
        }
    }
}
